package com.example.xmppclient;

import java.util.ArrayList;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ArrayList<String> messages = new ArrayList();
	private Handler mHandler = new Handler();
	private SettingsDialog mDialog;
	private EditText mRecipient;
	private EditText mSendText;
	private ListView mList;
	private XMPPConnection connection;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mRecipient = (EditText) this.findViewById(R.id.recipient);
        mSendText = (EditText) this.findViewById(R.id.sendText);
        mList = (ListView) this.findViewById(R.id.listMessages);
        setListAdapter();
        
        ///
        mRecipient.setText("bysycle@appspot.com");
        
        // Dialog for getting the xmpp settings
        mDialog = new SettingsDialog(this);
        
        //set a listener to show the settings dialog
        Button setup = (Button) this.findViewById(R.id.setup);
        setup.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mHandler.post(new Runnable(){
					public void run(){
						mDialog.show();
					}
				});
			}
        	
        });
        
        //set a listener to send a chat text message
        Button send = (Button) this.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String to = mRecipient.getText().toString();
				String text = mSendText.getText().toString();
				
				Log.i("XMPPClient", "Sending text ["+text+"] to ["+to+"]");
				Message msg = new Message(to,Message.Type.chat);
				msg.setBody(text);
				connection.sendPacket(msg);
				messages.add(connection.getUser()+":");
				messages.add(text);
				setListAdapter();
			}
		});
    }

    private void setListAdapter() {
		// TODO Auto-generated method stub
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.message_view, messages);
		mList.setAdapter(adapter);
	}

	public void setConnection(XMPPConnection connection) {
		// TODO Auto-generated method stub
		this.connection = connection;
		
		if(connection != null){
			//Add a packet listener to get messages sent to us
			PacketFilter filter =new MessageTypeFilter(Message.Type.chat);
			connection.addPacketListener(new PacketListener(){

				@Override
				public void processPacket(Packet packet) {
					// TODO Auto-generated method stub
					Message message = (Message) packet;
					if(message.getBodies()!=null){
						String fromName = StringUtils.parseBareAddress(message.getFrom());
						Log.i("XMPPClient","Got text ["+message.getBody()+"] from ["+fromName+"]");
						
						messages.add(fromName+":");
						messages.add(message.getBody());
						//add the incoming message to the list view
						mHandler.post(new Runnable(){
							public void run(){
								setListAdapter();
							}
						});
					}
				}
				
			}, filter);
		}
	}
}
